#include "app_lvglDeal.h"
#include "stdio.h"
#include "main.h"

lvglPar userLvglPar = {
    .screenIndex = SCREEN_MAIN,      // 当前屏幕序号
    .allNodeNum = 16,                // 可绑定节点数量
    .bindDevNum = 5,                 // 已绑定节点数量
    .devStatus = 0,                  // 节点设备状态
    .voltage = 4000,                 // 电池电压
    .deadline = 1742523342,          // 设备服务截止日期
    .linkDevNum = "104730780",       // 网关设备号
    .mainDevNum = "081610354",       // 设备号
    .iccid = "898604E81023C0187472", // sim卡iccid
    .software = "v1.0.0",            // 软件版本
    .hardware = "v1.0.0",            // 硬件版本
    .mainTemp = -30.0,               // 主界面温度
    .mainHumi = 0.0                  // 主界面湿度
};
void lv_label_refresh(void)
{
    lv_obj_t *conts[] = {
        guider_ui.screen_nodeinfo_cont_1,
        guider_ui.screen_nodeinfo_cont_2,
        guider_ui.screen_nodeinfo_cont_3,
        guider_ui.screen_nodeinfo_cont_4,
        guider_ui.screen_nodeinfo_cont_5,
        guider_ui.screen_nodeinfo_cont_6};
    if (userLvglPar.screenIndex == SCREEN_MAIN)
    {
        char temp[6] = {0};
        char Humi[6] = {0};
        sprintf(temp, "%5.1f", userLvglPar.mainTemp);
        sprintf(Humi, "%5.1f", userLvglPar.mainHumi);

        lv_label_set_text(guider_ui.screen_main_label_3, temp);
        lv_obj_set_style_bg_opa(guider_ui.screen_main_label_3, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_label_set_text(guider_ui.screen_main_label_2, Humi);
        lv_obj_set_style_bg_opa(guider_ui.screen_main_label_2, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    }
    if (userLvglPar.screenIndex == SCREEN_DEVINFO)
    {
        char devInfo[20] = {0};
        // 网关设备号
        sprintf(devInfo, "%s", userLvglPar.linkDevNum);
        lv_label_set_text(guider_ui.screen_devinfo_label_10, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_10, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        // iccid
        memset(devInfo, 0, sizeof(devInfo));
        sprintf(devInfo, "%s", userLvglPar.iccid);
        lv_label_set_text(guider_ui.screen_devinfo_label_11, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_11, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        // 服务费截止日期
        memset(devInfo, 0, sizeof(devInfo));
        struct tm *tm_info = localtime(&userLvglPar.deadline);
        strftime(devInfo, sizeof(devInfo), "%Y-%m-%d", tm_info);
        lv_label_set_text(guider_ui.screen_devinfo_label_12, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_12, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        // 可绑定节点数量
        memset(devInfo, 0, sizeof(devInfo));
        sprintf(devInfo, "%d", userLvglPar.allNodeNum);
        lv_label_set_text(guider_ui.screen_devinfo_label_13, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_13, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        // 已绑定节点数量
        memset(devInfo, 0, sizeof(devInfo));
        sprintf(devInfo, "%d", userLvglPar.bindDevNum);
        lv_label_set_text(guider_ui.screen_devinfo_label_14, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_14, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        // 软件版本
        memset(devInfo, 0, sizeof(devInfo));
        sprintf(devInfo, "%s", userLvglPar.software);
        lv_label_set_text(guider_ui.screen_devinfo_label_15, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_15, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        // 硬件版本
        memset(devInfo, 0, sizeof(devInfo));
        sprintf(devInfo, "%s", userLvglPar.hardware);
        lv_label_set_text(guider_ui.screen_devinfo_label_16, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_16, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        // 电池电压
        memset(devInfo, 0, sizeof(devInfo));
        sprintf(devInfo, "%dmV", userLvglPar.voltage);
        lv_label_set_text(guider_ui.screen_devinfo_label_17, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_17, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        // 报警类型
        memset(devInfo, 0, sizeof(devInfo));
        sprintf(devInfo, "%d", 0);
        lv_label_set_text(guider_ui.screen_devinfo_label_18, devInfo);
        lv_obj_set_style_bg_opa(guider_ui.screen_devinfo_label_18, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
    }
    if (userLvglPar.screenIndex == SCREEN_NODEINFO)
    {
        if (LVGL_labels != NULL)
        {
            guider_ui.screen_nodeinfo_cont_1 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_1, lv_color_hex(0x9efff6), 0, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_2 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_2, lv_color_hex(0x9efff6), 160, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_3 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_3, lv_color_hex(0x9efff6), 320, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_4 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_4, lv_color_hex(0x9efff6), 0, 140, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_5 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_5, lv_color_hex(0x9efff6), 160, 140, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_6 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_6, lv_color_hex(0x9efff6), 320, 140, 160, 140, 1);

            char *dataBuf[][6] = {
                {"01", "-12.0", "35.7", "081611097", "正常", "2025-03-26 16:55:59"},
                {"02", "15.8", "15.5", "081610651", "正常", "2025-03-26 16:55:59"},
                {"03", "-1.0", "67.0", "081610396", "离线", "2025-03-26 16:55:59"},
                {"04", "10.0", "20.6", "081610990", "报警", "2025-03-26 16:55:59"},
                {"05", "13.2", "79.2", "081610644", "正常", "2025-03-26 16:55:59"},
                {"06", "", "", "", "", ""}};

            for (int i = 0; i < 6; i++)
            {
                LVGL_labels[i][0] = create_label_info(conts[i], LVGL_labels[i][0], dataBuf[i][0], &lv_font_SourceHanSerifSC_Regular_26, 4, 4, 32, 27, 1);     // 序号
                LVGL_labels[i][1] = create_label_info(conts[i], LVGL_labels[i][1], dataBuf[i][1], &lv_font_SourceHanSerifSC_Regular_26, 34, 27, 71, 27, 1);   // 温度
                LVGL_labels[i][2] = create_label_info(conts[i], LVGL_labels[i][2], dataBuf[i][2], &lv_font_SourceHanSerifSC_Regular_26, 46, 55, 56, 26, 1);   // 湿度
                LVGL_labels[i][3] = create_label_info(conts[i], LVGL_labels[i][3], dataBuf[i][3], &lv_font_SourceHanSerifSC_Regular_16, 37, 4, 90, 16, 1);    // 设备号
                LVGL_labels[i][4] = create_label_info(conts[i], LVGL_labels[i][4], dataBuf[i][4], &lv_font_SourceHanSerifSC_Regular_16, 40, 95, 64, 18, 1);   // 设备状态
                LVGL_labels[i][5] = create_label_info(conts[i], LVGL_labels[i][5], dataBuf[i][5], &lv_font_SourceHanSerifSC_Regular_12, 14, 120, 129, 14, 1); // 节点时间
            }
        }
    }

    if (userLvglPar.screenIndex == SCREEN_NODEINFO_TWO)
    {
        if (LVGL_labels != NULL)
        {
            guider_ui.screen_nodeinfo_cont_1 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_1, lv_color_hex(0x9efff6), 0, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_2 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_2, lv_color_hex(0x9efff6), 160, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_3 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_3, lv_color_hex(0x9efff6), 320, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_4 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_4, lv_color_hex(0x9efff6), 0, 140, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_5 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_5, lv_color_hex(0x9efff6), 160, 140, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_6 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_6, lv_color_hex(0x9efff6), 320, 140, 160, 140, 1);

            char *dataBuf[][6] = {
                {"07", "", "", "", "", ""},
                {"08", "", "", "", "", ""},
                {"09", "", "", "", "", ""},
                {"10", "", "", "", "", ""},
                {"11", "", "", "", "", ""},
                {"12", "", "", "", "", ""}};
            for (int i = 0; i < 6; i++)
            {
                LVGL_labels[i][0] = create_label_info(conts[i], LVGL_labels[i][0], dataBuf[i][0], &lv_font_SourceHanSerifSC_Regular_26, 4, 4, 32, 27, 1); // 序号
            }

            for (int i = 0; i <  6; i++)
            {
                LVGL_labels[i][1] = create_label_info(conts[i], LVGL_labels[i][1], dataBuf[i][1], &lv_font_SourceHanSerifSC_Regular_26, 34, 27, 71, 27, 1);   // 温度
                LVGL_labels[i][2] = create_label_info(conts[i], LVGL_labels[i][2], dataBuf[i][2], &lv_font_SourceHanSerifSC_Regular_26, 46, 55, 56, 26, 1);   // 湿度
                LVGL_labels[i][3] = create_label_info(conts[i], LVGL_labels[i][3], dataBuf[i][3], &lv_font_SourceHanSerifSC_Regular_16, 37, 4, 90, 16, 1);    // 设备号
                LVGL_labels[i][4] = create_label_info(conts[i], LVGL_labels[i][4], dataBuf[i][4], &lv_font_SourceHanSerifSC_Regular_16, 40, 95, 64, 18, 1);   // 设备状态
                LVGL_labels[i][5] = create_label_info(conts[i], LVGL_labels[i][5], dataBuf[i][5], &lv_font_SourceHanSerifSC_Regular_12, 14, 120, 129, 14, 1); // 节点时间
            }
        }
    }

    if (userLvglPar.screenIndex == SCREEN_NODEINFO_THREE)
    {
        if (LVGL_labels != NULL)
        {
            guider_ui.screen_nodeinfo_cont_1 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_1, lv_color_hex(0x9efff6), 0, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_2 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_2, lv_color_hex(0x9efff6), 160, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_3 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_3, lv_color_hex(0x9efff6), 320, 0, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_4 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_4, lv_color_hex(0x9efff6), 0, 140, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_5 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_5, lv_color_hex(0x9efff6), 160, 140, 160, 140, 1);
            guider_ui.screen_nodeinfo_cont_6 = create_contSet_info(guider_ui.screen_nodeinfo, guider_ui.screen_nodeinfo_cont_6, lv_color_hex(0x9efff6), 320, 140, 160, 140, 1);

            char *dataBuf[][6] = {
                {"13",  "", "", "", "", ""},
                {"14",  "", "", "", "", ""},
                {"15", "", "", "", "", ""},
                {"16", "", "", "", "", ""},
                {"",  "", "", "", "", ""},
                {"", "", "", "", "", ""}};
            for (int i = 0; i < 6; i++)
            {
                LVGL_labels[i][0] = create_label_info(conts[i], LVGL_labels[i][0], dataBuf[i][0], &lv_font_SourceHanSerifSC_Regular_26, 4, 4, 32, 27, 1); // 序号
            }
            for (int i = 0; i <  6; i++)
            {
                LVGL_labels[i][1] = create_label_info(conts[i], LVGL_labels[i][1], dataBuf[i][1], &lv_font_SourceHanSerifSC_Regular_26, 34, 27, 71, 27, 1);   // 温度
                LVGL_labels[i][2] = create_label_info(conts[i], LVGL_labels[i][2], dataBuf[i][2], &lv_font_SourceHanSerifSC_Regular_26, 46, 55, 56, 26, 1);   // 湿度
                LVGL_labels[i][3] = create_label_info(conts[i], LVGL_labels[i][3], dataBuf[i][3], &lv_font_SourceHanSerifSC_Regular_16, 37, 4, 90, 16, 1);    // 设备号
                LVGL_labels[i][4] = create_label_info(conts[i], LVGL_labels[i][4], dataBuf[i][4], &lv_font_SourceHanSerifSC_Regular_16, 40, 95, 64, 18, 1);   // 设备状态
                LVGL_labels[i][5] = create_label_info(conts[i], LVGL_labels[i][5], dataBuf[i][5], &lv_font_SourceHanSerifSC_Regular_12, 14, 120, 129, 14, 1); // 节点时间
            }
        }
    }
}
